package jm.algorithm.recursion;

import java.util.Stack;

/**
 * @Description 递归转非递归
 * @date 2022/5/18 9:43
 */
public class Recursion2Normal {

    public static void main(String[] args) {
        usingVar(5);
    }

    private static void recursion(int n){
        if (n < 1) return;
        recursion(n -1);
        int v = 10 + n;
        System.out.println(v);
    }

    /**
     * 使用栈转为非递归
     * @param n
     */
    private static void usingStack(int n){
        Stack<Frame> frames = new Stack<>();
        // 模拟调用
        while (n > 0){
            frames.push(new Frame(n,n + 10));
            n--;
        }
        // 模拟返回
        while (!frames.isEmpty()){
            System.out.println(frames.pop().v);
        }
    }

    private static class Frame{
        int n;
        int v;

        public Frame(int n, int v) {
            this.n = n;
            this.v = v;
        }
    }


    private static void usingVar(int n){
        for (int i = 1; i <= n; i++) {
            System.out.println(i + 10);
        }
    }
}
